package com.yfbao.horizon.demo;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinioTest {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            // 创建 MinIO 客户端，连接到 MinIO 服务
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://10.2.8.159:9000")
                    .credentials("5kr48VMThiDPdgwm5zcz", "NWVCXmQs5QerhrDWZyRqnMzML5JEYgmp8ptjDqMl")
                    .build();

            // 检查存储桶是否存在，如果不存在则创建
//            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket("your-bucket-name").build());
//            if (!bucketExists) {
//                minioClient.makeBucket(MakeBucketArgs.builder().bucket("your-bucket-name").build());
//            }

            String fileName = "D:\\致远工作\\v8\\服务治理\\监控面板\\minio_rev1.json";
            File file = new File(fileName);
            // 上传文件到存储桶
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket("test-bucket")
                    .object("testfile")
                    .stream(new FileInputStream(file),file.length(),5*1024*1024)
                    .build());

            System.out.println("文件上传成功！");
        } catch (MinioException e) {
            System.out.println("错误发生：" + e);
        }
    }
}
